package com.giveu.signature.module.template.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.giveu.common.page.PageBuilder;
import com.giveu.common.page.PageInfoBT;
import com.giveu.common.page.QueryMap;
import com.giveu.signature.common.service.BaseController;
import com.giveu.signature.conts.CertificationLevel;
import com.giveu.signature.conts.CryptoStandard;
import com.giveu.signature.conts.DigestAlgorithm;
import com.giveu.signature.conts.KeystoreType;
import com.giveu.signature.conts.RenderingMode;
import com.giveu.signature.conts.SignAuth;
import com.giveu.signature.module.template.model.SignTemplateRelation;
import com.giveu.signature.module.template.model.SignatureConfig;
import com.giveu.signature.module.template.service.SignatureConfigService;

/**
 * <p>
 * 签章信息配置 前端控制器
 * </p>
 *
 * @author zhaoruding
 * @since 2019-11-12
 */
@Controller
@RequestMapping("/signature")
public class SignatureConfigController extends BaseController{
	
	private static final Logger log = LoggerFactory.getLogger(SignTemplateHtmlController.class);

	private static final String PREFIX = "module/template/sign/";
	
	@Autowired
	SignatureConfigService signatureConfigService;

	@RequestMapping("")
	private String index(Model model) {
		model.addAttribute("signAuths", SignAuth.values());
		return PREFIX + "index";
	}
	
	@RequestMapping("signEdit")
	private String signEdit(Model model) {
		List<SignatureConfig> signList = signatureConfigService.signObjects();
		model.addAttribute("entity", new SignTemplateRelation());
		model.addAttribute("signList", signList);
		return PREFIX + "signEdit";
	}
	
	@RequestMapping("/edit/{id}")
	private String edit(@PathVariable("id") Long id,Model model) {
		SignatureConfig entity = signatureConfigService.get(id);
		model.addAttribute("entity", entity==null?new SignatureConfig():entity);
		model.addAttribute("certificationLevels", CertificationLevel.values());
		model.addAttribute("digestAlgorithms", DigestAlgorithm.values());
		model.addAttribute("keystoreTypes", KeystoreType.values());
		model.addAttribute("renderingModes", RenderingMode.values());
		model.addAttribute("signAuths", SignAuth.values());
		model.addAttribute("cryptoStandards", CryptoStandard.values());
		return PREFIX + "edit";
	}
	
	@RequestMapping("/modify/{id}")
	private String modify(@PathVariable("id") Long id,Model model) {
		SignatureConfig entity = signatureConfigService.get(id);
		if(entity!=null){
			entity.setId(null);
			entity.setModify(true);
			entity.setSignVersion(entity.getSignVersion()+1);
		}
		model.addAttribute("entity", entity==null?new SignatureConfig():entity);
		model.addAttribute("certificationLevels", CertificationLevel.values());
		model.addAttribute("digestAlgorithms", DigestAlgorithm.values());
		model.addAttribute("keystoreTypes", KeystoreType.values());
		model.addAttribute("renderingModes", RenderingMode.values());
		model.addAttribute("signAuths", SignAuth.values());
		return PREFIX + "edit";
	}

	@RequestMapping("/list")
	@ResponseBody
	private Object list(SignatureConfig entity) {
	    QueryMap<SignatureConfig> queryMap = PageBuilder.buildQueryMap();
	    queryMap.setEntity(entity);
	    PageInfo<SignatureConfig> pageInfo = signatureConfigService.listByPage(queryMap);
	    return new PageInfoBT<>(pageInfo);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	private Object save(SignatureConfig entity) {
		signatureConfigService.save(entity);
	    return SUCCESS_TIP;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	private Object delete(SignatureConfig entity) {
		signatureConfigService.delete(entity);
		return SUCCESS_TIP;
	}
	
	@RequestMapping("/submit")
	@ResponseBody
	private Object submit(SignatureConfig entity) {
		entity.setSignStatus(1);
		try {
			signatureConfigService.save(entity);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS_TIP;
	}
}
